﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AnyThinkAds.Api;


public class TopOnAPlatform : IAdPlatform
{
    private string VideoUnitId = ""; 
    private string InterstitialUnitId = "";
    private Action _onAdCompleted;

    public TopOnAPlatform(string appId, string appKey, string videoAdId, string interAdId)
    {
        this.VideoUnitId = videoAdId;
        this.InterstitialUnitId = interAdId;
        ATSDKAPI.setLogDebug(true);
        ATSDKAPI.initSDK(appId, appKey);
        this.InitVideoAutoAd();
        this.InitPlacementAutoAd();
    }

    public override void NavigateToScene(Action onSuccess, Action<string> onFail)
    {
        // 无
    }

    // 插屏广告
    public override void ShowInterstitialAd(string adId)
    {
        ATInterstitialAutoAd.Instance.showAutoAd(InterstitialUnitId);
    }

    // 激励视频广告
    public override void ShowRewardedVideoAd(string adId, Action onSuccess, Action<string> onFail)
    {
        this._onAdCompleted = onSuccess;
        ATRewardedAutoVideo.Instance.showAutoAd(adId);
    }

    private void InitVideoAutoAd()
    {
        ATRewardedAutoVideo.Instance.client.onAdLoadEvent += onAdLoad;
        ATRewardedAutoVideo.Instance.client.onAdLoadFailureEvent += onAdLoadFail;
        ATRewardedAutoVideo.Instance.client.onAdVideoStartEvent += startVideoPlayback;
        ATRewardedAutoVideo.Instance.client.onAdVideoEndEvent += endVideoPlayback;
        ATRewardedAutoVideo.Instance.client.onAdVideoFailureEvent += failVideoPlayback;
        ATRewardedAutoVideo.Instance.client.onAdClickEvent += onAdClick;
        ATRewardedAutoVideo.Instance.client.onRewardEvent += onRewardEvent;
        ATRewardedAutoVideo.Instance.client.onAdVideoCloseEvent += onAdClose;
        string[] jsonList = { VideoUnitId };
        ATRewardedAutoVideo.Instance.addAutoLoadAdPlacementID(jsonList);
    }

    void InitPlacementAutoAd()
    {
        ATInterstitialAutoAd.Instance.client.onAdLoadEvent += onAdLoad;
        ATInterstitialAutoAd.Instance.client.onAdLoadFailureEvent += onAdLoadFail;
        ATInterstitialAutoAd.Instance.client.onAdShowEvent += onShow;
        ATInterstitialAutoAd.Instance.client.onAdClickEvent += onAdClick;
        ATInterstitialAutoAd.Instance.client.onAdCloseEvent += onAdClose;
        ATInterstitialAutoAd.Instance.client.onAdShowFailureEvent += onAdShowFail;
        ATInterstitialAutoAd.Instance.client.onAdVideoStartEvent += startVideoPlayback;
        ATInterstitialAutoAd.Instance.client.onAdVideoEndEvent += endVideoPlayback;
        ATInterstitialAutoAd.Instance.client.onAdVideoFailureEvent += failVideoPlayback;

        string[] jsonList = { InterstitialUnitId };
        ATInterstitialAutoAd.Instance.addAutoLoadAdPlacementID(jsonList);
    }

    private void onAdShowFail(object sender, ATAdErrorEventArgs erg)
    {
        Debug.LogError("显示异常:" + erg.placementId);
    }

    public void onRewardEvent(object sender, ATAdEventArgs erg)
    {
        this._onAdCompleted?.Invoke();
    }


    public void onAdLoad(object sender, ATAdEventArgs erg)
    {
       
    }

    public void onAdLoadFail(object sender, ATAdErrorEventArgs erg)
    {
       
    }

    public void onShow(object sender, ATAdEventArgs erg)
    {
      
    }

    public void onAdClick(object sender, ATAdEventArgs erg)
    {
        
    }

    public void onAdClose(object sender, ATAdEventArgs erg)
    {
        
    }

    public void startVideoPlayback(object sender, ATAdEventArgs erg)
    {
     
    }


    public void endVideoPlayback(object sender, ATAdEventArgs erg)
    {
        
    }

    public void failVideoPlayback(object sender, ATAdEventArgs erg)
    {
        
    }

}
